package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaMeetAnnounceEntity;
import org.edu.modules.oa.entity.VeOaMeetEntity;
import org.edu.modules.oa.entity.VeOaMeetUsersEntity;
import org.edu.modules.oa.entity.VeOaScheduleEntity;
import org.edu.modules.oa.mapper.*;
import org.edu.modules.oa.service.VeOaPublicService;
import org.edu.modules.oa.vo.VeOaMeetCheckTableVo;
import org.edu.modules.oa.vo.VeOaMyJoinMeetVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class VeOaPublicServiceImpl implements VeOaPublicService {
    @Resource
    private IVeOaScheduleMapper iVeOaScheduleMapper;

    @Resource
    private IVeOaSchedulExcleVoMapper iVeOaSchedulExcleVoMapper;

    @Resource
    private IVeOaMeetCheckMapper iVeOaMeetCheckMapper;// 会议审核表Mapper

    @Resource
    private IVeOaMeetUsersMapper veOaMeetUsersMapper;// 会议人员Mapper

    @Resource
    private IVeOaMyJoinMeetMapper iVeOaMyJoinMeetMapper;

    @Resource
    private IVeOaMeetUsersMapper iVeOaMeetUsersMapper;

    @Resource
    private IVeOaCheckMeetAnnounceMapper iVeOaCheckMeetAnnounceMapper;

    @Override
    public HashMap<String, Object> showMySchedule(VeOaScheduleEntity veOaScheduleEntity) {
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<VeOaScheduleEntity> wrapper = new QueryWrapper();

        if (null != veOaScheduleEntity.getTitle()) {
            wrapper.like("title", veOaScheduleEntity.getTitle());
        }

        wrapper.eq("IS_DELETED", 0);
        wrapper.eq("create_user", user.getId());
        wrapper.orderByDesc("create_time");
        //获取集合
        List<VeOaScheduleEntity> list = iVeOaScheduleMapper.selectList(wrapper);
        map.put("list", list);
        return map;
    }

    @Override
    public HashMap<String, Object> showMeetCheckTable(VeOaMeetCheckTableVo veOaMeetCheckTableVo) {
        // 查询会议审核表格
        List<VeOaMeetCheckTableVo> voList=iVeOaMeetCheckMapper.showTableNoPage(veOaMeetCheckTableVo);

        for ( VeOaMeetCheckTableVo tableVo  :voList){
            QueryWrapper<VeOaMeetUsersEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("meet_id", tableVo.getId());
            List<VeOaMeetUsersEntity> list = veOaMeetUsersMapper.selectList(wrapper);
            String userName = "";
            for (VeOaMeetUsersEntity v : list) {
                userName += v.getUserName()+",";
            }
            tableVo.setUserName(userName);
            tableVo.setMeetTime(tableVo.getStartTime()+"至"+tableVo.getEndTime());
        }

        // 把查出来的东西存到map里面去
        HashMap<String , Object> map = new HashMap<>();
        map.put("list",voList);
        // 查询表格总行数
        return map;
    }

    @Override
    public HashMap<String, Object> ShowMyJoinMeet(VeOaMeetEntity veOaMeetEntity) {

        HashMap<String,Object>map = new HashMap<>();
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String title = "'%"+veOaMeetEntity.getTitle().trim()+"%'";
        List<VeOaMyJoinMeetVo>myJoinMeetVoList = iVeOaMyJoinMeetMapper.selectMeetById(0,0,title,veOaMeetEntity.getPlaceId(),"'"+user.getId()+"'");

        // 把查出来的东西存到map里面去
        map.put("list",myJoinMeetVoList);
        return map;
    }

    @Override
    public HashMap<String, Object> showCheckMeetAnnounce(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity) {
        HashMap<String,Object> map = new HashMap<>();

        //创建查询的条件，wrapper是条件综合器
        QueryWrapper<VeOaMeetAnnounceEntity>wrapper = new QueryWrapper<>();
        //前台传来的条件
        if(veOaMeetAnnounceEntity.getMeetName()!=null && veOaMeetAnnounceEntity.getMeetName()!="");{
            wrapper.like("meet_name",veOaMeetAnnounceEntity.getMeetName());
        }
        if(veOaMeetAnnounceEntity.getTitle()!=null && veOaMeetAnnounceEntity.getTitle()!=""){
            wrapper.like("title",veOaMeetAnnounceEntity.getTitle());
        }
        //mybatis plus的动态查询方法
        // 条件加上分页查询
        List<VeOaMeetAnnounceEntity>list = iVeOaCheckMeetAnnounceMapper.selectList(wrapper);
        int count = iVeOaCheckMeetAnnounceMapper.selectCount(wrapper);
        // 把查出来的东西存到map里面去
        map.put("list",list);
        return map;
    }
}
